package com.awccis.driver.service.impl;

import com.awccis.admin.model.pay.PaySimpleInfoBean;
import com.awccis.admin.service.AdminService;
import com.awccis.common.utils.Constants;
import com.awccis.common.utils.JsonUtil;
import com.awccis.customer.model.order.OrderBean;
import com.awccis.driver.dao.DriverDao;
import com.awccis.driver.dao.DriverOrderDao;
import com.awccis.driver.model.*;
import com.awccis.driver.model.car.CarInfo;
import com.awccis.driver.model.credit.CreditBean;
import com.awccis.driver.model.order.HistoryOrderBean;
import com.awccis.driver.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;

/**
 * @Author: 纯牛奶丶
 * @Date: 2018-03-30 0030 上午 11:05
 * @Since: REDMI.txt.0
 * @Decription: driver司机端数据交互接口实现类
 **/
@Service("DriverServiceImpl")
public class DriverServiceImpl implements DriverService {
    
    @Autowired
    public DriverDao driverDao;
    @Autowired
    public DriverOrderDao driverOrderDao;
    @Resource
    private AdminService adminService;
    
    
    /**
     * 判断登录名是否存在
     *
     * @param nickName 登录名
     * @return true or false true表示存在可以登录 false 不存在
     */
    @Override
    public boolean selectLoginNameExists(int nickName) {
        return driverDao.selectDriverExists(nickName) == 1;
    }
    
    /**
     * 用户登陆
     *
     * @param driverLoginInfo driverLoginInfo
     * @return driverInfo
     */
    @Override
    public DriverInfo checkDriverInfo(DriverLoginInfo driverLoginInfo) {
        return driverDao.checkDriverInfo(driverLoginInfo);
    }

    /**
     * 根据司机id获取手机号
     * @param nickName nickName
     * @return driverPhone
     */
    @Override
    public String getDriverPhoneByNickName(int nickName) {
        return driverDao.getDriverPhoneByID(nickName);
    }

    /**
     * 注册用户信息
     *
     * @param driverInfo driverInfo
     * @return int
     */
    @Override
    public int insertDriverInfo(DriverInfo driverInfo) {
        int driverId = 0;
        if (driverInfo.getDriverName() != null || !driverInfo.getDriverName().equals("")) {
            driverId = driverDao.insertDriverInfo(driverInfo);
        }
        return driverId;
    }
    
//    /**
//     * 判断是否已经登陆
//     *
//     * @param nickName 登录名
//     * @return true or false  true表示在线，false表示不在线
//     */
//    @Override
//    public boolean selectLoginStatus(int nickName) {
//        return driverDao.selectDriverIsOnline(nickName) == 0;
//    }
    
    /**
     * 获取上一个司机的nickName
     *
     * @return int
     */
    @Override
    public int getLastNickName() {
        int nickName = 0;
        if(driverDao.selectLastDriverNickName() == null){
            nickName = 0;
        }else {
            nickName = driverDao.selectLastDriverNickName();
        }
        return nickName;
    }
    
    /**
     * 添加汽车信息和初始化司机的位置
     * @param carInfo carInfo 汽车信息
     * @return true or false
     */
    @Override
    public boolean insertCarInfoAndLocation(CarInfo carInfo) {
        boolean flag = false;
        if (carInfo.getCarBrand() != null && !"".equals(carInfo.getCarBrand()) && carInfo.getDriverId() != null) {
            boolean b1 = driverDao.insertCarInfo(carInfo) == 1;
            boolean b2 = driverDao.insertDriverLocation(carInfo.getDriverId()) == 1;
            if(b1 && b2){
                flag = true;
            }
        }
        return flag;
    }
    
//    /**
//     * 更改在线状态
//     *
//     * @param statusBean statusBean
//     * @return boolean 更改状态的结果
//     */
//    @Override
//    public boolean updateIsOnlineStatus(StatusBean statusBean) {
//        return driverDao.updateIsOnlineStatus(statusBean) == 1;
//    }
    
//    /**
//     * 更改运营状态
//     *
//     * @param statusBean statusBean
//     * @return boolean 更能改运营状态的结果
//     */
//    @Override
//    public boolean updateIsWorkStatus(StatusBean statusBean) {
//        return driverDao.updateIsWorkStatus(statusBean) == 1;
//    }
    
    /**
     * 更改冻结状态
     *
     * @param statusBean statusBean
     * @return boolean true or false
     */
    @Override
    public boolean updateStatus(StatusBean statusBean) { return driverDao.updateStatus(statusBean) == 1; }
    
    /**
     * 更改审核状态
     *
     * @param statusBean statusBean
     * @return boolean true or false
     */
    @Override
    public boolean updateIsChecked(StatusBean statusBean) { return driverDao.updateIsChecked(statusBean) ==1; }
    
    /**
     * 根据司机的driverNickName更改信用积分
     *
     * @param statusBean statusBean
     * @return boolean更改信用积分的结果
     */
    @Override
    public boolean updateIntegralCount(StatusBean statusBean) {
        return driverDao.updateIntegralCount(statusBean) == 1;
    }

    /**
     * 添加司机的详情积分
     *
     * @param creditBean creditBean
     * @return true or false
     */
    @Override
    public boolean insertDriverCredit(CreditBean creditBean) {
        return driverDao.insertDriverCredit(creditBean) == 1;
    }

    /**
     * 根据司机的driverNickName查询信用积分
     *
     * @param statusBean statusBean
     * @return 返回司机的星级
     */
    @Override
    public Integer selectIntegralCount(StatusBean statusBean) {
        
        try{
            int count = driverDao.selectStarLevel(statusBean.getDriverId());
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 5;
        }

    }

    /**
     * 根据driverID 查询星级
     *
     * @param driverId driverID
     * @return 星
     */
    @Override
    public Integer selectStarLevel(Integer driverId) {
        return driverDao.selectStarLevel(driverId);
    }

    /**
     * 查询司机当前订单
     *
     * @param driverInfo 传入的司机的nickName
     * @return 订单信息
     */
    @Override
    public OrderBean selectCurrentOrder(DriverInfo driverInfo) {
        return driverOrderDao.selectCurrentOrder(driverInfo);
    }
    
    /**
     * 查询司机的个人信息
     *
     * @param driverLoginInfo 传入的值为司机的nickName
     * @return 返回的是司机的driverInfo
     */
    @Override
    public DriverInfo selectDriverInfo(DriverLoginInfo driverLoginInfo) {
        return driverDao.selectDriverInfo(driverLoginInfo);
    }
    
    /**
     * 更改订单的不同状态和时间
     * 首先判断传入的时间类型，根据类型来执行不同的插入操作
     *
     * @param orderBean 传入的是时间和订单
     * @return 返回更改结果
     */
    @Override
    public boolean updateDifferOrderTime(OrderBean orderBean) {
        String typeTime = orderBean.getTimeType();
        int result = 0;
        switch (typeTime) {
            case Constants.DRIVER_ORDER_RECEIVE_TIME:
                orderBean.setOrderReceiveTime(new Date());
                result = driverOrderDao.insertReceiveOrderInfo(orderBean);
                break;
            case Constants.DRIVER_ORDER_TRAVEL_ONTIME:
                orderBean.setOrderTravelOnTime(new Date());
                result = driverOrderDao.insertTravelOnTimeInfo(orderBean);
                break;
            case Constants.DRIVER_ORDER_TRAVEL_END_TIME:
                orderBean.setOrderTravelEndTime(new Date());
                result = driverOrderDao.insertTravelEndTimeInfo(orderBean);
                break;
            case Constants.DRIVER_ORDER_FINISH_TIME:
                orderBean.setOrderFinishTime(new Date());
                result = driverOrderDao.insertOrderFinishInfo(orderBean);
                break;
            default:
                break;
        }
        return result == 1;
    }
    
    /**
     * 查询历史订单总数
     *
     * @param hashMap hashMap
     * @return 历史订单总数
     */
    @Override
    public Integer selectDriverOrderCount(HashMap hashMap) {
        String totalCount = "totalCount" ;
        int counts = 0;
        
        if (hashMap.get(totalCount) != null) {
            counts = driverOrderDao.selectDriverOrdersCount(hashMap);
        }
        return counts;
    }
    
    /**
     * 查询历史订单
     *
     * @param hashMap 司机的信息
     * @return 订单的list
     */
    @Override
    public List<HistoryOrderBean> selectHistoryOrders(HashMap<String, Object> hashMap) {
        List<HistoryOrderBean> list = new ArrayList<>();
        String startCount = "startCount";
        String limitCounts = "limitCounts";
        
        if(hashMap.get(startCount) != null && hashMap.get(limitCounts) != null){
            list = driverOrderDao.selectHistoryOrders(hashMap);
        }
        
        return list;
    }
    
//    /**
//     * 更改司机的位置信息
//     *
//     * @param locationBean
//     * @return
//     */
//    @Override
//    public boolean updateDriverLocation(LocationBean locationBean) {
//        return driverDao.updateDriverLocation(locationBean) == 1;
//    }

    /**
     * 查询司机的简单信息
     *
     * @param hashMap driverId
     * @return hashMap
     */
    @Override
    public HashMap selectSimpleDriverInfo(HashMap hashMap) {
        return driverDao.selectSimpleDriverInfo(hashMap);
    }
    
    /**
     * 记录访问信息
     * @param map 1
     */
    @Override
    public void recordVisitInfo(Map<String, Object> map) {
        try {
            String path = JsonUtil.getJsonConfigValue("visitInfoLogPath");
            if (!new File(path).exists()) {
                new File(path).mkdirs();
            }
            File file = new File(path, "driverVisitInfo.log");
            if(!file.exists()){
                file.createNewFile();
            }

            BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
            br.write(String.valueOf(map.get("date")) + "," + map.get("os") + "," + map.get("model") + "," +map.get("ip")+"\n");
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过driverId更改司机的详细信息
     *
     * @param driverUpdateInfo driverUpdateInfo
     * @return 更改结果
     */
    @Override
    public Boolean updateDriverDetailInfoById(DriverUpdateInfo driverUpdateInfo) {
        boolean result = false;
        if(driverUpdateInfo.getDriverId() != 0){
            result = driverDao.updateDriverDetailInfo(driverUpdateInfo) == 2;
        }
        return result;
    }

    /**
     * 通过driverId获取未完成订单信息
     * @param driverId driverid
     * @return List
     */
    @Override
    public List<NoFinishOrder> getNoFinishOrders(int driverId) {
        return driverDao.getOrderStutusE4(driverId);
    }

    /**
     * 司机获取自己本周需要缴费金额
     *
     * @param paySimpleInfoBean driverId payWeek
     * @return paySimpleInfoBean
     */
    @Override
    public PaySimpleInfoBean getNeedPayInfo(PaySimpleInfoBean paySimpleInfoBean) {
        double exchange = adminService.getExchangeRate();
        PaySimpleInfoBean simpleInfoBean = driverDao.selectNeedPayInfo(paySimpleInfoBean);
        if(simpleInfoBean!=null){
            simpleInfoBean.setSumCost(exchange*simpleInfoBean.getSumCost());
            simpleInfoBean.setNeedPay(exchange*simpleInfoBean.getNeedPay());
        }
        return simpleInfoBean;
    }

    /**
     * 司机确认自己交费
     *
     * @param paySimpleInfoBean paySimpleInfoBean
     * @return 更改结果
     */
    @Override
    public Boolean setNeedPay(PaySimpleInfoBean paySimpleInfoBean) {
        return driverDao.updateNeedPay(paySimpleInfoBean) == 1;
    }

    /**
     * 查询缴费记录
     * @param driverId driverId
     * @return List<PayRecord>
     */
    @Override
    public List<PayRecord> getPayRecord(int driverId) {
        return driverDao.getPayRecord(driverId);
    }
}
